import {createRouter, createWebHistory} from 'vue-router'
import workApi from "@/http/workApi";
import {useUserStore} from "@/stores/user";

// import LoginView from "@/views/LoginView.vue";
// import StudentView from "@/views/student/StudentView.vue";
// import AdminView from "@/views/admin/AdminView.vue";
// import TeacherView from "@/views/teacher/TeacherView.vue";
// import SuperAdmin from "@/views/superAdmin/SuperAdmin.vue";
// import UserInfo from "@/views/UserInfo.vue";
//
// import TrainView from "@/views/teacher/TrainView.vue";
// import Organization from "@/views/admin/Organization.vue";
// import Teacher from "@/views/admin/Teacher.vue";
// import ClassView from "@/views/admin/ClassView.vue";
// import Student from "@/views/admin/Student.vue";
// import TrainManagement from "@/views/admin/TrainManagement.vue";
// import ClassManage from "@/views/teacher/ClassManage.vue";
// import Resources from "@/views/superAdmin/Resources.vue";
// import SuperAdminTask from "@/views/superAdmin/Task.vue";
// import ProjectView from "@/views/student/project/ProjectView.vue";
// import StudentIndexView from "@/views/student/StudentIndexView.vue";
// import StudentTask from "@/views/student/project/task/Task.vue";
// import Analysis from "@/views/student/project/task/Analysis.vue";
// import Execute from "@/views/student/project/task/Execute.vue";
// import Grade from "@/views/student/project/task/Grade.vue";
// import GradeManage from "@/views/teacher/GradeManage.vue";
// import ResourceCentre from "@/views/teacher/ResourceCentre.vue";


const LoginView = () => import("@/views/LoginView.vue")

const UserInfo = () => import("@/views/UserInfo.vue")
const StudentView = () => import("@/views/student/StudentView.vue")
const AdminView = () => import("@/views/admin/AdminView.vue")
const TeacherView = () => import("@/views/teacher/TeacherView.vue")
const SuperAdmin = () => import("@/views/superAdmin/SuperAdmin.vue")


const TrainView = () => import("@/views/teacher/TrainView.vue")
const Organization = () => import("@/views/admin/Organization.vue")
const Teacher = () => import("@/views/admin/Teacher.vue")
const ClassView = () => import("@/views/admin/ClassView.vue")
const Student = () => import("@/views/admin/Student.vue")
const TrainManagement = () => import("@/views/admin/TrainManagement.vue")
const ClassManage = () => import("@/views/teacher/ClassManage.vue")
const Resources = () => import("@/views/superAdmin/Resources.vue")
const SuperAdminTask = () => import("@/views/superAdmin/Task.vue")
const ProjectView = () => import("@/views/student/project/ProjectView.vue")
const StudentIndexView = () => import("@/views/student/StudentIndexView.vue")
const StudentTask = () => import("@/views/student/project/task/Task.vue")
const Analysis = () => import("@/views/student/project/task/Analysis.vue")
const Execute = () => import("@/views/student/project/task/Execute.vue")
const Grade = () => import("@/views/student/project/task/Grade.vue")
const GradeManage = () => import( "@/views/teacher/GradeManage.vue")
const ResourceCentre = () => import("@/views/teacher/ResourceCentre.vue")


const StudentIndexSecond = () => import("@/views/student/StudentIndexSecond.vue")
const StudentIndexFirst = () => import("@/views/student/StudentIndexFirst.vue")
const StudentProjectSecond = () => import("@/views/student/project/StudentProjectSecond.vue")
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'Login',
            component: LoginView,
        },
        {
            path: '/user',
            name: 'User',
            component: UserInfo,
            children: [
                {
                    path: 'password',
                    name: 'Password',
                    component: UserInfo
                }
            ]
        },
        {
            path: '/student',
            name: 'Student',
            component: StudentView,
            children: [
                {
                    path: 'index',
                    name: 'StudentIndex',
                    component: StudentIndexView
                },
                {
                    path: 'first/index',
                    name: 'StudentIndexFirst',
                    component: StudentIndexFirst
                },
                {
                    path: 'second/index',
                    name: 'StudentIndexSecond',
                    component: StudentIndexSecond,
                },
                {
                    path: 'second/project',
                    name: 'StudentProjectSecond',
                    component: StudentProjectSecond,
                },
                {
                    path: 'project',
                    name: 'Project',
                    component: ProjectView,
                    children: [
                        {
                            path: 'task',
                            name: 'StudentTask',
                            component: StudentTask
                        },
                        {
                            path: 'analysis',
                            name: 'Analysis',
                            component: Analysis
                        },
                        {
                            path: 'execute',
                            name: 'Execute',
                            component: Execute
                        },
                        {
                            path: 'grade',
                            name: 'Grade',
                            component: Grade
                        },
                    ]
                }
            ]
        },
        {
            path: '/admin',
            name: 'Admin',
            component: AdminView,
            children: [
                {
                    path: 'organization',
                    name: 'OrganizationManage',
                    component: Organization
                },
                {
                    path: 'teacher',
                    name: 'TeacherManage',
                    component: Teacher
                },
                {
                    path: 'class',
                    name: 'ClassView',
                    component: ClassView
                },
                {
                    path: 'student',
                    name: 'StudentManage',
                    component: Student
                },
                {
                    path: 'teaching',
                    name: 'TrainManagement',
                    component: TrainManagement
                },
            ]
        },
        {
            path: '/teacher',
            name: 'Teacher',
            component: TeacherView,
            children: [
                {
                    path: 'train',
                    name: 'TrainManage',
                    component: TrainView
                },
                {
                    path: 'class',
                    name: 'ClassManage',
                    component: ClassManage
                },
                {
                    path: 'gradeManage',
                    name: 'GradeManage',
                    component: GradeManage
                },
                {
                    path: 'resourceCentre',
                    name: 'ResourceCentre',
                    component: ResourceCentre
                },
            ]
        },
        {
            path: '/super_admin',
            name: 'superAdmin',
            component: SuperAdmin,
            children: [
                {
                    path: 'resources',
                    name: 'Resources',
                    component: Resources,
                },
                {
                    path: 'task',
                    name: 'Task',
                    component: SuperAdminTask,
                },
            ]
        }
    ]
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
    const user = useUserStore()
    // 目标不是`根路径`并且`验证暂时不通过`
    if (to.path !== '/' && !user.isVerification) {
        workApi.get(
            'token'
        ).then(res => {
            if (res.success) {
                // 验证token成功放行
                user.setIdentity(res.who)
                user.setIsVerification()
                user.setInfo({
                    name: res.name,
                    phone: res.phone,
                    account: res.account,
                    head: res.head,
                })
                next()
            } else {
                next({name: 'Login'})
            }
        }).catch(r => {
            console.log(r)
            next({name: 'Login'})
        });
    } else {
        next()
    }
})
export default router
